Evaluation of statement-level breakpoints

ABSTRACT

A method for evaluating a breakpoint in a line of source code includes encountering the breakpoint and evaluating whether the breakpoint is at the start of the line. If the breakpoint is at the start of the line, then the debugger stops at the breakpoint and tracks the breakpoint. If the breakpoint is not at the start of the line, then the debugger evaluates whether the breakpoint is reached via a back branch. If the breakpoint is not at the start of the line and is reached via a back branch, then the debugger stops at the breakpoint and tracks the breakpoint. If the breakpoint is not at the start of the line and is not a back branch target, then the debugger passes the breakpoint and tracks the breakpoint.

TECHNICAL FIELD

This disclosure relates to debugger breakpoints. In particular, itrelates to a debugger that evaluates breakpoints efficiently andconsistently on a line of source code.

BACKGROUND

A debugger is a computer program used to detect and correct errors in atarget computer program. Most debuggers have a feature that allows auser to stop execution of the target computer program and analyzevariables in the program. A user may stop execution of the program bysetting a breakpoint at a desired location in the program and allowingthe program to run up to the breakpoint.

Typically, a debugger will implement a software breakpoint through anopcode replacement, through which the opcode of an instruction may bereplaced with an invalid opcode or an opcode specifically designated asa breakpoint opcode. When the program runs and the debugger encountersthe breakpoint, the invalid or breakpoint opcode may cause an exception.The debugger gains control of the program, handles the exception, andstops execution of the program until a user resumes the program. Whenthe breakpoint is removed, the displaced opcode is replaced.

When a user sets a breakpoint on a line of source code, the debuggercreates the breakpoint at the beginning of the first statementassociated with the source line. When program execution comes to thebreakpoint, the breakpoint fires and program execution halts before anystatements on the source line are executed. A line of source code maycontain multiple statements, with each statement corresponding to one ormore machine instructions generated by the compiler. When the compilergenerates debuggable code for the debugger, as opposed to optimizedcode, it does so that the debugger can stop at statement boundaries andretrieve variables. If the debugger was to stop in the middle of astatement, the variable value may not be accurate, as the statementwould not be executed through the machine instructions associated withthat statement, and variable values may reside in registers andtemporary memory locations. When a breakpoint is set on a statement, itis set on the first instruction associated with the statement.

SUMMARY

In embodiments of the invention, a method for setting and evaluating aline breakpoint on a line of source code includes setting a breakpointat the start of each statement within the line, encountering one of thebreakpoints and determining whether the breakpoint is at the start ofthe line or was reached as a result of a back branch. If the breakpointis at the start of the line, then the debugger stops at the breakpointand tracks the breakpoint. If the breakpoint is not at the start of theline, then the debugger determines whether the breakpoint is reached viaa back branch. If the breakpoint is not at the start of the line and isreached via a back branch, then the debugger stops at the breakpoint andtracks the breakpoint. If the breakpoint is not at the start of the lineand is not reached via a back branch, then the debugger passes thebreakpoint and tracks the breakpoint.

In another embodiment of the invention a method for debugging a line ofsource code in a program includes setting breakpoints on the line,running the program, and encountering a current breakpoint. The debuggerdetermines whether the current breakpoint is at the start of the line.If the current breakpoint is at the start of the line, then the debuggerstops at the current breakpoint, tracks the current breakpoint, andreturns to running the program when a user sends a command to resumeprogram execution. If the current breakpoint is not at the start of theline, then the debugger determines whether the breakpoint is reached viaa back branch. If the current breakpoint is not at the start of the lineand is reached via a back branch, then the debugger stops at the currentbreakpoint, tracks the current breakpoint, and returns to running theprogram when a user sends a command to resume program execution. If thebreakpoint is not at the start of the line and is not reached via a backbranch, then the debugger tracks the breakpoint and returns toencountering a current breakpoint.

BRIEF DESCRIPTION OF THE DRAWINGS

The drawings included in the present application are incorporated into,and form part of, the specification. They illustrate embodiments of thepresent invention and, along with the description, serve to explain theprinciples of the invention. The drawings are only illustrative oftypical embodiments of the invention and do not limit the invention.

FIGS. 1A, 1B, and 1C are examples of operations involving debuggers andbreakpoints. FIG. 1A is a diagram of program execution where the loopstatement loops back to a statement that does not contain a breakpoint.FIG. 1B is a diagram of program execution where the loop statement loopsback to a statement that does contain a breakpoint. FIG. 1C is a diagramof program execution where the debugger sets a breakpoint on everystatement.

FIG. 2 is a diagram of a method for evaluating a breakpoint in a line ofsource code, according to embodiments of the invention.

FIG. 3 is a diagram of a method for evaluating a breakpoint in a line ofsource code using a breakpoint record, according to embodiments of theinvention.

FIG. 4 is a diagram of a method for debugging a line of source code,according to embodiments of the invention.

FIG. 5 is a diagram of a method for debugging a line of source codeusing a breakpoint record, according to embodiments of the invention.

FIG. 6 is an example of a method for evaluating breakpoints on severallines of source code using a breakpoint record, according to embodimentsof the invention.

DETAILED DESCRIPTION

A line of source code may include a branch instruction that loops backto an earlier point on the line so that the branch's target instructionand any instructions in the loop are executed multiple times until acondition is met. This branch back to a previous instruction on the linemay be referred to as a “back branch”, and its target instruction as a“back branch target”. There are different methods of executing a backbranch, such as while and do while loops, that may lead to the sameoperational result. However, when a breakpoint is placed on a linehaving a loop instruction, the breakpoint may cause inconsistentdebugging operation due to the method in which the loop is executed. Aloop instruction may or may not branch back to the instruction havingthe breakpoint, depending on the form of the loop instruction.

For example, consider the following two loops in a line of source code,in which a breakpoint is placed on the first statement in each line:

Logically, in the while loop above, the condition is evaluated first andthe function is executed. In practice, the debugger encounters themachine instructions associated with the while statement every iterationof the function. Compilers generated while statements as if they werecoded as follows:

 if(i!=3) {  do   foo(&i)  while(i!=3) }

When a breakpoint is set on the line containing the while loop, thebreakpoint is placed on the first instruction of the while statement, asit is the first statement of the line. The debugger may only hit thebreakpoint before it executes the line, and may only execute thefunction statement during a loop iteration, not the while statement, asexecution does not flow through the first machine instruction associatedwith the first statement as the loop iterates.

Logically, in the do while loop, the function is executed first and thecondition evaluated. In contrast to the while loop, the breakpoint isplaced on the function statement for the do while loop, as it is thefirst statement of the line. This may lead to the function executingevery time the loop iterates, and the debugger hitting the breakpoint oneach loop iteration. This inconsistent operation of the debugger forloop statements may lead to a loss of control for most users that arenot aware of how the debugger works or the program is compiled.

An alternative to setting a breakpoint by line may be to set astatement-level breakpoint on every statement of a line. Under such aset-up, a user may retain control over the debugger program. However, itis inefficient and time-consuming to step through every statement on aline. As a tool, a debugger should be consistent so as to be reliableand efficient so as to be useful.

FIGS. 1A, 1B, and 1C are examples of operations involving debuggers andbreakpoints that illustrate current debugger operations. Each wordL|X|ST|Y represents a statement within a line, with the “X” representingthe line number and the “Y” representing the statement number in theline. The arrows show the flow of the program through the four lines.There is one loop statement, L2ST3, which loops back for 3 iterations.Corresponding to lines with a line breakpoint are statementsbreakpoints, denoted by dashed vertical lines in front of the statementon which they are placed. Line breakpoints have been placed on lines 1,2, and 4. There is no line breakpoint on line 3.

FIG. 1A is a diagram of program execution where the loop statement loopsback to a statement that does not contain a breakpoint. The debuggersets a breakpoint on the first statement of every line for which a userhas set a breakpoint. The debugger stops at L1ST1, as it has abreakpoint. After the user continues the program execution, the debuggerstops at L2ST1, as it has a breakpoint. When the user executes theremaining instructions on line 2, the debugger loops back to L2ST2 threetimes, but does not stop, as there is no breakpoint on L2ST2. Thedebugger continues through line 3, as it does not have a breakpoint seton the line, and stops at L4ST1, as it contains a breakpoint. The totalnumber of times the debugger stops is 3—once on each line having abreakpoint.

FIG. 1B is a diagram of program execution where the loop statement loopsback to a statement that does contain a breakpoint. The process is thesame as above up until the debugger encounters the loop statement,L2ST3. In this instance, L2ST3 loops back to L2ST1, which contains abreakpoint. The debugger stops at the breakpoint, and the user maycontinue program execution. The debugger will stop for all threeiterations, after which it will continue to L4ST1 and stop. The totalnumber of times the debugger stops is 6—once for each line having abreakpoint and three times for each iteration of the loop.

FIG. 1C is a diagram of program execution where the debugger sets abreakpoint on every statement. In this instance, loop operation isconsistent for different loop statements. However, as every statement ona breakpoint line contains a breakpoint, the debugger will stop at everystatement, including for all three repeats of the loop. The total numberof times the debugger stops is 15—once for each statement on abreakpoint line and twice for each iteration of the loop.

Debugger Method for Evaluating a Breakpoint

According to embodiments of the invention, a debugger may efficientlyevaluate a breakpoint so that the debugger exhibits consistentbreakpoint behavior on a line of code. The debugger may track executionof instructions on a line so that the debugger may consistently stop atcertain locations and for certain operations, while allowing the user toparse through code without stopping at every statement. The debugger maystop at the beginning of a line of source code and at the target of aback branch, such as a loop, within the line of source code. A debuggerutilizing this method may exhibit consistent behavior for loopstatements having different forms.

FIG. 2 is a diagram of a method for evaluating a breakpoint on a line ofsource code, according to embodiments of the invention. The line ofsource code may contain a statement-level breakpoint on every statementin the line. When a debugger encounters a breakpoint, as in 201, itdetermines whether the breakpoint is at the start of the line, as in202. If the breakpoint is at the start of the line, the debugger stopsat the breakpoint, as in 203, and tracks the breakpoint, as in 204. Ifthe breakpoint is not at the start of the line, the debugger determineswhether the breakpoint statement is reached via a back branch, as in205. If the breakpoint statement is reached via a back branch, thedebugger stops at the breakpoint, as in 203, and tracks the breakpoint,as in 204. If the breakpoint statement is not reached via a back branch,the debugger passes the breakpoint, as in 206, and tracks thebreakpoint, as in 204.

By tracking which statements have been previously executed on a line,the debugger may stop when it reaches a new line or encounters apreviously executed instruction on the line. After the debugger stops,it may reset its tracking record so that it will only stop once forevery iteration of the loop or when it runs to the next breakpoint on anew line. This method allows a user to predictably stop at everyiteration of a loop, regardless of the loop method or point to which theloop branches back to. At the same time, the user does not have to parsethrough source code, statement by statement.

The determinations of whether a breakpoint is at the start of a line, asin 202, and whether the breakpoint statement is reached via a backbranch, as in 205, may be interchanged or performed simultaneously,along with their corresponding conclusions. The relevant methoddeterminations are whether a breakpoint starts a line, whether abreakpoint is reached via a back branch, and whether a breakpoint isneither of those two, regardless of the order in which they aredetermined.

FIG. 3 is a diagram of a method for evaluating a statement-levelbreakpoint in a line of source code using a breakpoint record, accordingto embodiments of the invention. In 201, a program encounters abreakpoint. The breakpoint may be an opcode replace or other breakpointmethod that modifies an instruction on a line of code.

In 301, a debugger accesses a breakpoint record associated with the linebreakpoint for which the statement-level breakpoints have been set. Thebreakpoint record tracks which statement-level breakpoints have beenencountered for a line of source code. The breakpoint record may be adata structure that holds the executed statements of the line of sourcecode. There is a breakpoint record for each line of code. Alternatively,if it is desired that code be evaluated in groups of five lines, abreakpoint record could be associated with a group of five lines, withthe debugger making stops corresponding to the beginning of a group andback branches in the group.

In 302, the debugger determines whether the breakpoint record is empty.The breakpoint record is empty if it does not contain any breakpointsassociated with the line. This includes a breakpoint record that has nobreakpoints at all or no breakpoints corresponding to the line of sourcecode. If the breakpoint record for a line is empty, then the breakpointmay be at the start of the line, as no breakpoints have yet beenencountered. If the breakpoint record is empty, the debugger may stop atthe breakpoint, as in 203. After the debugger stops execution of theprogram, it may reset the breakpoint record, as in 303. Resetting thebreakpoint record may cause the debugger to only stop at the beginningof a back branch, such as a loop, without stopping again at everystatement in the iteration. Resetting the breakpoint record may involveclearing the breakpoint record of all breakpoints associated with theline or, if the breakpoint record is empty, maintaining an emptybreakpoint record. The breakpoint is added to the breakpoint record, asin 304.

If the breakpoint record is not empty, the debugger may evaluate whetherthe breakpoint is in the breakpoint record, as in 305. If the breakpointis in the breakpoint record, then the breakpoint has already beenencountered, as in a back branch, and the debugger may stop at thebreakpoint, as in 203. After the debugger stops execution of theprogram, it may reset the breakpoint record, as in 303, and add thebreakpoint to the breakpoint record, as in 304. If the breakpoint is notin the breakpoint record, then the debugger passes the breakpoint, as in206, and adds the breakpoint to the breakpoint record, as in 304.

Debugger Method for Evaluating Lines of Code

FIG. 4 is a diagram of a method for debugging a line of source code,according to embodiments of the invention. A user places a linebreakpoint on one or more lines, and the debugger sets statementbreakpoints on each statement of the selected lines, as in 401. The userthen runs the program with a debugger, as in 402.

When the program encounters a breakpoint, as in 201, the debuggerdetermines whether the breakpoint is at the start of the line, as in202. If the breakpoint is at the start of the line, the debugger stopsat the breakpoint, as in 203, and tracks the breakpoint, as in 204A.When the user runs the program again, as in 402, the program returns toexecuting until the debugger encounters a breakpoint, as in 201. If thebreakpoint is not at the start of the line, the debugger determineswhether the breakpoint is reached via a back branch, as in 205. If thebreakpoint is reached via a back branch, the debugger stops at thebreakpoint, as in 203, tracks the breakpoint, as in 204A, and continuesprogram execution when the user runs the program again, as in 402. Ifthe breakpoint is not reached via a back branch, the debugger passes thebreakpoint, as in 206, and tracks the breakpoint, as in 204B. Theprogram continues executing instructions until the debugger encounters abreakpoint, as in 201.

FIG. 5 is a diagram of a method for debugging a line of source code,according to embodiments of the invention. A user places a linebreakpoint on one or more lines of a program, and a debugger placesstatement breakpoints on each statement of the selected lines, as in401. The user then runs the program, as in 402.

In 201, the program encounters a breakpoint. In 301, the debuggeraccesses a breakpoint record associated with the line breakpoint forwhich the statement-level breakpoints have been set. In 302, thedebugger determines whether the breakpoint record is empty. If thebreakpoint record is empty, the debugger may stop at the breakpoint, asin 203. After the debugger stops execution of the program, it may resetthe breakpoint record, as in 303. The breakpoint is added to thebreakpoint record, as in 304A. When the user runs the program again, asin 402, the program returns to executing until the program encounters abreakpoint, as in 201.

If the breakpoint record is not empty, the debugger may determinewhether the breakpoint is in the breakpoint record, as in 305. If thebreakpoint is in the breakpoint record, the debugger may stop at thebreakpoint, as in 203. After the debugger stops execution of theprogram, it may reset the breakpoint record, as in 303, add thebreakpoint to the breakpoint record, as in 304A, and continue programexecution when the user runs the program again, as in 402. If thebreakpoint is not in the breakpoint record, then the debugger passes thebreakpoint, as in 206, and adds the breakpoint to the breakpoint record,as in 304B. The program continues executing instructions until thedebugger encounters a breakpoint, as in 201.

Exemplary Method

FIG. 6 is an example of a method for evaluating breakpoints on severallines of source code using a breakpoint record, according to embodimentsof the invention. Each word L|X|ST|Y represents a statement within aline, with the “X” standing for the line number and the “Y” standing forthe statement number in the line. The arrows show the flow of theprogram through the four lines. There is one statement, L2ST3, whichloops back to L2ST2 for one iteration. The table below the program flowcontains entries for each statement, each breakpoint record of thelines, and each debugger action upon encountering a statement with abreakpoint. Line breakpoints have been placed on lines 1, 2, and 4.Corresponding to lines with a line breakpoint are statements breakpoints(step 401), denoted by dashed vertical lines in front of the statementon which they are placed. There is no line breakpoint on line 3. Allstep references are to FIG. 5.

The user starts execution of the program (step 402) and the debuggerencounters L1ST1 (step 201), which has a statement breakpoint. Thedebugger accesses the line 1 breakpoint record (step 301) and evaluateswhether the breakpoint record is empty (step 302). The breakpoint recordis empty, as statement L1ST1 is the first statement on the line. Thedebugger stops at L1ST1 (step 203) and resets the breakpoint record. Inthis case, the breakpoint record is already empty, so resetting thebreakpoint record involves keeping the breakpoint record for the line atits current condition. The debugger adds L1ST1 to the breakpoint record(304A). When the user resumes execution of the program (step 402), thedebugger encounters L1ST2 (step 201), which has a statement breakpoint.The debugger accesses the line 1 breakpoint record (step 301) andevaluates whether the breakpoint record is empty (step 302). Thebreakpoint record is not empty, as L1ST1 was added by the debugger inthe previous debugger action. The debugger evaluates whether the L1ST2breakpoint is in the breakpoint record (step 305). The only breakpointin the breakpoint record is L1ST1, so the debugger passes L1ST2 withoutstopping (step 206) and adds L1ST2 to the breakpoint record (step 304B).L1ST3 is evaluated similarly to L1ST2.

In Line 2, the debugger encounters L2ST1 (step 201) and accesses theline 2 breakpoint record (301). The line 2 breakpoint record is empty,as it is not associated with the line 1 breakpoint record. The debuggercontinues to handle the breakpoint at L2ST1 like L1ST1 above and thebreakpoints at L2ST2 and L2ST3 like L1ST2 above. However, L2ST3 loopsback to L2ST2. The debugger encounters L2ST2 for a second time (step201) and accesses the line 2 breakpoint record (step 301). In contrastto the first time L2ST2 breakpoint was encountered, the debugger findsL2ST2 in the breakpoint record, as it was previously encountered andadded to the breakpoint record. The debugger stops at L2ST2 (step 203)and resets the breakpoint record for line 2 (step 303). Unlike the firststatement of the line, where the breakpoint record was empty, resettingthe breakpoint record after a back branch involves clearing thebreakpoint record. After the breakpoint record is reset, L2ST2 is addedto the breakpoint record.

When the user resumes execution of the program (402), the programcontinues execution through line 2, as the loop only looped back once,and continues to line 3. Line 3 does not have a line breakpoint, and socontains no statement breakpoints. The debugger encounters nobreakpoints, and continues to line 4, which is treated in the same wayas line 1, which contained no back branches. The total number of timesthe debugger stopped execution of the program was 4—once per linebreakpoint and once per loop iteration. If the loop had gone through 3iterations, as in the example of FIGS. 1A, 1B, and 1C, the debuggerusing the method described above would have stopped 6 times, regardlessof the form of the loop and significantly less than astop-at-every-statement method.

Although the present invention has been described in terms of specificembodiments, it is anticipated that alterations and modificationsthereof will become apparent to those skilled in the art. Therefore, itis intended that the following claims be interpreted as covering allsuch alterations and modifications as fall within the true spirit andscope of the invention.

What is claimed is:
 1. A method for evaluating a breakpoint in a line ofsource code in a program, comprising: encountering the breakpoint;determining whether the breakpoint is at a start of the line; if thebreakpoint is at the start of the line, then stopping at the breakpoint,retrieving and analyzing variables in the program, and tracking thebreakpoint; if the breakpoint is not at the start of the line, thendetermining whether the breakpoint is reached via a back branch; if thebreakpoint is reached via a back branch, then stopping at thebreakpoint, retrieving and analyzing variables in the program, andtracking the breakpoint; and if the breakpoint is not reached via a backbranch and is not at the start of the line, then passing the breakpointwithout retrieving and analyzing variables in the program and trackingthe breakpoint.
 2. The method of claim 1, further comprising accessing abreakpoint record for the line of source code.
 3. The method of claim 2,wherein: determining whether the breakpoint is at the start of the linecomprises determining whether the breakpoint record is empty; anddetermining whether the breakpoint is reached via a back branchcomprises determining whether the breakpoint is in the breakpointrecord.
 4. The method of claim 3, wherein: tracking the breakpoint ifthe breakpoint is at the start of the line comprises: resetting thebreakpoint record; adding the breakpoint to the breakpoint record afterthe breakpoint record has been reset; tracking the breakpoint if thebreakpoint is not at the start of the line and is a back branchcomprises: resetting the breakpoint record; adding the breakpoint to thebreakpoint record after the breakpoint record has been reset; andtracking the breakpoint if the breakpoint is not at the start of theline and is not a back branch comprises adding the breakpoint to thebreakpoint record.
 5. The method of claim 4, wherein resetting thebreakpoint record if the breakpoint is not at the start of the line andis reached via a back branch comprises clearing the breakpoint record.6. A method for debugging a line of source code in a program with adebugger, comprising: setting breakpoints on the line; running theprogram; encountering a current breakpoint; determining whether thecurrent breakpoint is at a start of the line; if the current breakpointis at the start of the line, then: stopping at the current breakpoint;tracking the current breakpoint; returning to running the program when auser sends a command to resume program execution; if the currentbreakpoint is not at the start of the line, then determining whether thebreakpoint is reached via a back branch; if the current breakpoint isreached via a back branch, then: stopping at the current breakpoint;tracking the current breakpoint; returning to running the program whenthe user sends a command to resume program execution; if the breakpointis not reached via a back branch and is not at the start of the line,then: tracking the breakpoint; and returning to running the programwithout stopping at the current breakpoint.
 7. The method of claim 6,further comprising accessing a breakpoint record for the line of sourcecode.
 8. The method of claim 7, wherein: determining whether thebreakpoint is at the start of the line comprises determining whether thebreakpoint record is empty; and determining whether the breakpoint isreached via a back branch comprises determining whether the breakpointis in the breakpoint record.
 9. The method of claim 8, wherein: trackingthe breakpoint if the breakpoint is at the start of the line comprises:resetting the breakpoint record; adding the breakpoint to the breakpointrecord after the breakpoint record has been reset; tracking thebreakpoint if the breakpoint is not at the start of the line and is aback branch comprises: resetting the breakpoint record; adding thebreakpoint to the breakpoint record after the breakpoint record has beenreset; tracking the breakpoint if the breakpoint is not at the start ofthe line and is not a back branch comprises adding the breakpoint to thebreakpoint record.
 10. The method of claim 9, wherein resetting thebreakpoint record if the breakpoint is not at the start of the line andis reached via a back branch comprises clearing the breakpoint record.11. A system for debugging a line of source code in a program with adebugger, the system comprising a computer configured to perform amethod comprising: setting breakpoints on the line; running the program;encountering a current breakpoint; determining whether the currentbreakpoint is at a start of the line; if the current breakpoint is atthe start of the line, then: stopping at the current breakpoint;tracking the current breakpoint; returning to running the program when auser sends a command to resume program execution; if the currentbreakpoint is not at the start of the line, then determining whether thebreakpoint is reached via a back branch; if the current breakpoint isreached via a back branch, then: stopping at the current breakpoint;tracking the current breakpoint; returning to running the program whenthe user sends a command to resume program execution; if the breakpointis not reached via a back branch and is not at the start of the line,then: tracking the breakpoint; and returning to running the programwithout stopping at the current breakpoint.
 12. The system of claim 11,wherein the method further comprises accessing a breakpoint record forthe line of source code.
 13. The system of claim 12, wherein:determining whether the breakpoint is at the start of the line comprisesdetermining whether the breakpoint record is empty; and determiningwhether the breakpoint is reached via a back branch comprisesdetermining whether the breakpoint is in the breakpoint record.
 14. Thesystem of claim 13, wherein: tracking the breakpoint if the breakpointis at the start of the line comprises: resetting the breakpoint record;adding the breakpoint to the breakpoint record after the breakpointrecord has been reset; tracking the breakpoint if the breakpoint is notat the start of the line and is a back branch comprises: resetting thebreakpoint record; adding the breakpoint to the breakpoint record afterthe breakpoint record has been reset; tracking the breakpoint if thebreakpoint is not at the start of the line and is not a back branchcomprises adding the breakpoint to the breakpoint record.
 15. The systemof claim 14, wherein resetting the breakpoint record if the breakpointis not at the start of the line and is reached via a back branchcomprises clearing the breakpoint record.